/***************************************************************************
 *   Copyright (C) 2006 by Marco Bonifazi   *
 *   marcobonifazi@gmail.com   *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/
#include <GL/gl.h>	/* Core library*/
#include <GL/glu.h>	/* Function written using core library*/
#include <GL/glut.h>	/* Utility Toolkit, Windowing functions important for interactive applications.*/
#include "_gl.h"
#include <stdio.h>	/* The stdio header provides functions for performing input and output. */
#include <stdlib.h>	/* The stdlib header defines several general operation functions and macros. */
#include <string.h>	/* The string header provides manipulating strings functions */
#include <math.h>	/* The math header defines several mathematic functions. */
#include <time.h>	/* The time header provides several time and date functions . */

int display_list_creation = 0;

void segment(float* p1, float* p2){
    float x1 = p1[0];
    float y1 = p1[1];
    float x2 = p2[0];
    float y2 = p2[1];
   /*  printf("(%f, %f), (%f, %f)\n", x1, y1, x2, y2); */
    glBegin(GL_LINES);
    glVertex3f(x1, y1, 0);
    glVertex3f(x2, y2, 0);
    glEnd();

};

void create_quad(float* p1, float* p2, int display_list){
    float x1 = p1[0];
    float y1 = p1[1];
    float x2 = p2[0];
    float y2 = p2[1];
    
    glNewList(1, GL_COMPILE);
    glBegin(GL_QUADS);
    glVertex3f(x1, y1, 0);
    glVertex3f(x1, y2, 0);
    glVertex3f(x2, y2, 0);
    glVertex3f(x2, y1, 0);
    glEnd();
    glEndList();

};

void quads_list(int** coords_list, int size){
    int i;
    
    for (i=0; i < size; i++){
    	glPushMatrix();
    	glTranslatef(coords_list[i][0], coords_list[i][1], 0);
    	glCallList(1);
    	glPopMatrix();
    
    }
}



void create_display_list(){
    float p1[2] = { -0.5, -0.5 };
    float p2[2] = { 0.5, 0.5 };

    create_quad(p1, p2, 1);

};

void quadrato(){
    glCallList(1);
}

void trasla(float x, float y){
    glTranslatef(x, y, 0);
}

void opengl_display(float ratio){
   /* In this function there is the draw which we want to display */
    glMatrixMode(GL_MODELVIEW);
    
    if (display_list_creation == 0){
    	create_display_list();
    	display_list_creation = 1;
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glScalef( 1 , ratio, 1);
    glClear(GL_COLOR_BUFFER_BIT); /* Clears the drawing area*/
    glColor3f(1.0,0.0,1.0);
}

void opengl_init(int width, int height, int real_width, int real_height){
    display_list_creation = 0; /* In the display funcion this variable will be valued*/
    
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE ); 
    				/* Define RGB color mode and set the use of double buffer */
    glutInitWindowSize(width, height);	/* Sets window dimensions*/
    glutCreateWindow("Graph minimization - Marco Bonifazi");	/* Create a window on the screen */
    glMatrixMode(GL_PROJECTION);
    glViewport(0, 0, width*10, height*10);
    glLoadIdentity();
    glOrtho(-real_width, real_width, -real_height, real_height, -100.0, 100.0); /* Set the world coordinates */
    gluLookAt(0,0,1, 0,0,0, 0,10,0);
    glClearColor(0, 0, 0, 0); /* Sets clear color (rgba) */
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
